<?php
namespace Admin\Controller;
use Common\Controller\AdminController;

class ClassifyController extends AdminController{

	public function index(){
		$list = $this->getList('*', 'classify', null, 'sort desc,id asc');
		$this->sortNode($list, $result);
		$this->assign('list', $result);
		$this->display();
	}

	public function add(){
		if(IS_POST){
			$result = $this->updateData($_POST, 'classify');
			$this->returnResult($result);
		}else{
			$list = $this->getList('id,pid,name', 'classify', null, 'sort desc,id asc');
			$this->sortNode($list, $result);
			$this->assign('list', $result);
			$this->display();
		}
	}

	public function edit(){
		if(IS_POST){
			$_POST['sort'] = $_POST['set_sort'];
			$_POST['status'] = $_POST['set_status'];
			unset($_POST['set_sort']);
			unset($_POST['set_status']);
			$result = $this->updateData($_POST, 'classify',2);
			$this->returnResult($result);
		}else{
			$info = $this->getInfo('*', 'classify', array('id' => I('get.id', 0, 'intval')));
			if(is_null($info['pic'])){
				$info['pic'] = "";
			}
			if($info){
				$data['info'] = $info;
				$data['status'] = 1;
			}else{
				$data['info'] = '数据不存在';
				$data['status'] = 0;
			}
			$this->ajaxReturn($data);
		}
	}

	public function del(){
		$id = I('get.id', 0, 'intval');
		$newsNum = M('news')->where(array('pid' => $id))->count();
		if($newsNum > 0){
			$this->returnResult(false, '分类下有新闻，不可删除');
		}
		$subNum = M('classify')->where(array('pid' => $id))->count();
		if($subNum > 0){
			$this->returnResult(false, '请先删除子分类');
		}
		$result = $this->deleteData(array('id' => $id), 'classify');
		$this->returnResult($result);
	}
}